package com.dhcc.DRGsHosp.infra.util;

import java.util.Comparator;
import java.util.Map;

/**
 * @title		:MapComparatorImpl.java
 * @description	:maplist排序
 * @author		:lileizheng
 * @date		:2015年6月12日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
public class MapComparatorImpl implements Comparator<Map<String, Object>> {
	private String key;
	private String sort;
	public MapComparatorImpl(String key, String sort){
		this.key = key;
		this.sort = sort;
	}
	@Override
	public int compare(Map<String, Object> m1, Map<String, Object> m2) {
		int returnValue = 1;
		if(null!=m1 && null!=m2){
			double d1 = 0;
			double d2 = 0;
			try{
				d1 = m1.get(key)==null?0:Double.parseDouble(m1.get(key).toString());
				d2 = m2.get(key)==null?0:Double.parseDouble(m2.get(key).toString());
			}catch(Exception e){
//				e.printStackTrace();
				d1 = m1.get(key).hashCode();
				d2 = m2.get(key).hashCode();
			}
            if("desc".equals(sort)){
            	if(d1>d2){
                	returnValue = -1;
                }else if(d1==d2){
                	returnValue = 0;
                }else{
                	returnValue = 1;
                }
            } else if("asc".equals(sort)){
            	if(d1>d2){
                	returnValue = 1;
                }else if(d1==d2){
                	returnValue = 0;
                }else{
                	returnValue = -1;
                }
            }
        }
        return returnValue;
	}
}
